package com.redisdemo.demo.redisMq.util;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author  yuwc
 */
@Slf4j
@Component
public  class RedisClientUtil {

    @Value("${spring.redis.host}")
    private  String host;
    @Value("${spring.redis.password}")
    private  String password;
    @Value("${spring.redis.port}")
    private  int port;

    public RedisClient getRedisClient() {
        RedisClient redisClient = null;
        try {
            RedisURI redisUri = RedisURI.Builder.redis(host)
                    .withPort(port)
//                    .withPassword(password)
                    .withDatabase(0)
                    .build();

            redisClient = RedisClient.create(redisUri);
            return redisClient;
        } catch (Exception e) {
            log.error("获取redis链接异常", e);
        } finally {
            if (redisClient != null) {
                redisClient.shutdown();
            }

        }
        return null;
    }

}
